package com.bosch.in.rtp1.ept.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import com.bosch.in.rtp1.ept.constant.CommonConstants;
import com.bosch.in.rtp1.ept.constant.QueryKeyWord;
import com.bosch.in.rtp1.ept.constant.SkippedRecipeConstanst;
import com.bosch.in.rtp1.ept.model.dto.DurationColorCon;

/**
 * 
 * @author TPR1HC
 *
 */
public class StringUtility {	
	
	private static final String EMPTY = "";
	private static final String SELECT_ALL = "(all)";
	
	// condition to compare numeric value. If value is all positive, the condition is always unsatisfied.
	private static final String UNSATISFY_CONDITION = " < -1";
	private static final String DIFF_JAVA_OPERATOR = "!=";
	private static final String DIFF_TXT_OPERATOR = "<>";
	
	/**
	 * 
	 * @param patternsStr
	 * @return
	 */
	public static List<String> extractHCPatterns(String patternsStr) {
		List<String> result = new ArrayList<String>();
		if (patternsStr != null && !patternsStr.equals("")) {
			Pattern p = Pattern.compile(CommonConstants.DELIMITER, Pattern.LITERAL);
			result = Arrays.asList(p.split(patternsStr));
		} 
		return result;
	}
	
	/**
	 * 
	 * @param hcPatterns
	 * @return
	 */
	public static String convertHCPatternsToStr(List<String> hcPatterns) {
		StringBuffer patternBuf = new StringBuffer();
		if (hcPatterns.size() > 0) {
			patternBuf.append(hcPatterns.get(0));
			
			for (int i = 1; i < hcPatterns.size(); i++) {
				patternBuf.append(CommonConstants.DELIMITER);
				patternBuf.append(hcPatterns.get(i));
			}
		}
		return patternBuf.toString();
	}
	
	public static String convertHCPatternStr(String hcPatternStr) {
		if (hcPatternStr == null) {
			return "";
		}
		return hcPatternStr.replaceAll(CommonConstants.DELIMITER, CommonConstants.COMMA);
	}
	
	/**
	 * 
	 * @param patternsStr
	 * @return
	 */
	public static List<Integer> extractOrder(String orderList) throws NumberFormatException {
		Pattern p = Pattern.compile(CommonConstants.COMMA, Pattern.LITERAL);
		String[] orderStrs = p.split(orderList);
		Integer[] orderInts = new Integer[orderStrs.length];
		for (int i = 0; i < orderStrs.length; i++) {
			orderInts[i] = Integer.parseInt(orderStrs[i]);
		}
		return Arrays.asList(orderInts);
	}
	
	public static String addPercentageAtStartEnd(String in)
	{
		if (in != null)
		{
			return QueryKeyWord.PERCENTAGE+in+QueryKeyWord.PERCENTAGE;
		}
		return QueryKeyWord.PERCENTAGE;
	}
	
	/**
	 * Convert NULL String Object to EMPTY String Object
	 * @param String
	 * @return String
	 */
	public static String convertToEmptyIfNull(String in)	
	{
		return in == null ? EMPTY : in; 
	}
	
	/**
	 * Convert NULL String Object to SELECT_ALL String Object
	 * @param String
	 * @return String
	 */
	public static String convertToSelectAllIfNull(String in)	
	{
		return (in == null|| in.isEmpty()) ? SELECT_ALL : in; 
	}
	
	
	public static String arrayToString(String[] parameters) {
		String str = Arrays.toString(parameters);
		if (!str.equals("")) {
			str = str.substring(1, str.length() - 1);
		}
		return str;
	}
	
	public static String buildRedColorCondition(DurationColorCon durColCon) {
		String condition = "";
		if (durColCon.getRedColOp() == 0) {
			condition = UNSATISFY_CONDITION;
		} else if (SkippedRecipeConstanst.OPERATORS[durColCon.getRedColOp()].equals(DIFF_TXT_OPERATOR)) { // Replace <> by !=
			condition =  DIFF_JAVA_OPERATOR + QueryKeyWord.BLANK + durColCon.getRedColVal();
		} else {
			condition = SkippedRecipeConstanst.OPERATORS[durColCon.getRedColOp()] + QueryKeyWord.BLANK + durColCon.getRedColVal();
		}
		
		return condition;
	}
	
	public static String buildYellowColorCondition(DurationColorCon durColCon) {
		String condition = "";
		if (durColCon.getYellowColOp() == 0) {
			condition = UNSATISFY_CONDITION;
		} else if (SkippedRecipeConstanst.OPERATORS[durColCon.getYellowColOp()].equals(DIFF_TXT_OPERATOR)){ // Replace <> by !=
			condition =  DIFF_JAVA_OPERATOR + QueryKeyWord.BLANK + durColCon.getYellowColVal();
		} else {
			condition = SkippedRecipeConstanst.OPERATORS[durColCon.getYellowColOp()] + QueryKeyWord.BLANK + durColCon.getYellowColVal();
		}
		
		return condition;
	}
}
